﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using System.Data;
using DevExpress.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Models;
using Newtonsoft.Json;
using ElingOA6_Admin_DSJCJ.Areas.Admin.Models;
using System.Text;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 被评议人定义
    /// </summary>
    /// 
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class PeerReviewBeenVoterController : AdminBaseController
    {
        /// <summary>
        /// 定义部门业务逻辑类
        /// </summary>
        private BLL.HRDept hrDeptBLL = new BLL.HRDept();
        /// <summary>
        /// 人员岗位逻辑类
        /// </summary>
        private BLL.HREmplPosition hrEmplPositionBLL = new BLL.HREmplPosition();
        /// <summary>
        /// 人员逻辑类
        /// </summary>
        private BLL.HREmpl hrEmplBLL = new BLL.HREmpl();

        //
        // GET: /Admin/PeerReviewBeenVote/
        /// <summary>
        /// 被评议人定义
        /// </summary>
        /// <returns></returns>
        /// 
        [AuthorityActionFilter("m_PeerReviewBeenVoter", Order = 1)]
        [LogPageActionFilter("被评议人定义", Order = 2)]
        public ActionResult Index()
        {
            BLL.PeerReview peerReviewBll = new BLL.PeerReview();

            DataSet ds = peerReviewBll.GetIsFtatus();

            string isFstatus = "";
            if (ds.Tables[0].Rows.Count > 0)
            {
                isFstatus = ds.Tables[0].Rows[0]["FStatus"].ToString();
            }
            else
            {
                isFstatus = "";
            }
            ViewData["isFstatus"] = isFstatus;

            return View();
        }

        /// <summary>
        /// 被定义人选择页面
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult PeerReviewBeenVoterContent(string beenVoterClsCd, string index)
        {
            BLL.PeerReview peerReviewBll = new BLL.PeerReview();
            List<Model.PeerReview> peerReviewList = peerReviewBll.GetList();//只有1个主题
            foreach (Model.PeerReview model in peerReviewList) 
            {
                ViewData["FBeenVoterClsName"] = model.FTitle;
                ViewData["peerReviewID"] = model.FPeerReviewID;
            }
            ViewData["list"] = peerReviewList;

            BLL.PeerReviewResult peerReviewResultBll = new BLL.PeerReviewResult();
            DataSet ds = peerReviewResultBll.GetAllList();
            string visible = "false";
            if (ds.Tables[0].Rows.Count > 0)
            {
                visible = "true";
            }
            ViewData["visible"] = visible;


            BLL.PeerReviewBeenVoterCls peerReviewVoterClsBll = new BLL.PeerReviewBeenVoterCls();
            List<Model.PeerReviewBeenVoterCls> peerReviewBeenVoterClsAllList = peerReviewVoterClsBll.GetAll();//查出类别所有项

            if (peerReviewBeenVoterClsAllList.Count > 0)
            {
                ViewData["peerReviewBeenVoterClsAllList"] = peerReviewBeenVoterClsAllList;

                
                if (beenVoterClsCd != "" && beenVoterClsCd != null)
                {
                    ViewData["tableJsonStr"] = GetTableJsonStr("", "", beenVoterClsCd);
                    for (int i = 0; i < peerReviewBeenVoterClsAllList.Count; i++) 
                    {                       
                        if (peerReviewBeenVoterClsAllList[i].FBeenVoterClsCd == beenVoterClsCd) 
                        {
                            index = i.ToString();
                            ViewData["beenVoterClsCd"] = peerReviewBeenVoterClsAllList[i].FBeenVoterClsCd;
                        }
                    }
                }
                else 
                {
                    ViewData["beenVoterClsCd"] = peerReviewBeenVoterClsAllList[0].FBeenVoterClsCd;
                    ViewData["tableJsonStr"] = GetTableJsonStr("", "", peerReviewBeenVoterClsAllList[0].FBeenVoterClsCd);
                }
            }
            else 
            {
                ViewData["peerReviewBeenVoterClsAllList"] = new List<Model.PeerReviewBeenVoterCls>();

                ViewData["beenVoterClsCd"] = "";

                ViewData["tableJsonStr"] = "";
 
            }
            ViewData["index"] = index;

            return View();
        }


        /// <summary>
        /// 得到部门和人员
        /// </summary>
        /// <param name="returnValue">树选择的人员</param>
        /// <param name="peerReviewID">主题ID</param>
        /// <param name="beenVoterClsCd">类别编号</param>
        /// <returns>字符串</returns>
        public string GetTableJsonStr(string returnValue = "", string peerReviewID = "", string beenVoterClsCd = "")
        {
            string tableJsonStr = "";
            string entityNo = GlobalConstant.ENTITYNO;
            BLL.PeerReviewBeenVoter peerReviewBeenVoterBll = new BLL.PeerReviewBeenVoter();
            //按照类别
            List<Model.PeerReviewBeenVoter> peerReviewBeenVoterList = peerReviewBeenVoterBll.GetAllListByClsCd(entityNo, beenVoterClsCd);
            tableJsonStr += "[";
            if (returnValue == "")
            {
                if (peerReviewBeenVoterList.Count > 0)
                {
                    foreach (Model.PeerReviewBeenVoter model in peerReviewBeenVoterList)
                    {
                        tableJsonStr += "{";
                        tableJsonStr += "\"FEmpName\":\"" + model.FEmpName + "\",\"FEmpNo\":\"" + model.FBeenVoterEmplNo + "\",\"FDeptName\":\"" + model.deptName + "\",\"FDeptNo\":\"" + model.FBeenVoterDeptNo + "\",\"Value\":\"" + model.FBeenVoterEmplNo + "\"";
                        tableJsonStr += "},";
                    }
                    tableJsonStr = tableJsonStr.Substring(0, tableJsonStr.LastIndexOf(","));
                }
            }
            //选择的人员
            else if (returnValue != "")
            {
                List<SelectListItem> itemList = JsonConvert.DeserializeObject<List<SelectListItem>>(returnValue);
                List<Model.PeerReviewBeenVoter> peerReviewBeenVotorModelList = new List<Model.PeerReviewBeenVoter>();
                Model.PeerReviewBeenVoter peerReviewBeenVoterModel = new Model.PeerReviewBeenVoter();

                foreach (SelectListItem selectItem in itemList)
                {
                    peerReviewBeenVoterModel = new Model.PeerReviewBeenVoter();
                    peerReviewBeenVoterModel.FBeenVoterDeptNo = peerReviewBeenVoterBll.GetDeptNoByEmpNo(selectItem.Value);
                    peerReviewBeenVoterModel.FBeenVoterEmplNo = selectItem.Value;
                    peerReviewBeenVoterModel.FBeenVoterClsCd = beenVoterClsCd;
                    peerReviewBeenVoterModel.FPeerReviewID = decimal.Parse(peerReviewID);
                    peerReviewBeenVoterModel.FEntityNo = entityNo;
                    peerReviewBeenVotorModelList.Add(peerReviewBeenVoterModel);

                    tableJsonStr += hrEmplBLL.GetEmpNameAndDept(entityNo, selectItem.Value) + ",";
                }
                //保存
                try
                {
                    peerReviewBeenVoterBll.Add(peerReviewBeenVotorModelList, beenVoterClsCd, decimal.Parse(peerReviewID));
                }
                catch (Exception)
                {

                }
                if (itemList.Count > 0)
                {
                    tableJsonStr = tableJsonStr.Substring(0, tableJsonStr.LastIndexOf(","));
                }
            }
            tableJsonStr += "]";

            return tableJsonStr;
        }

    }
}
